package ldk.app.kotlinweibo.activities

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.support.v7.app.AppCompatActivity
import dagger.android.AndroidInjection
import kotlinx.android.synthetic.main.activity_welcome.*
import ldk.app.kotlinweibo.R
import ldk.app.kotlinweibo.configs.Session
import ldk.util.logger.LLog
import javax.inject.Inject

class WelcomeActivity : AppCompatActivity() {
    private val TAG = "WelcomeActivity"

    @Inject
    internal lateinit var mSession : Session

    private var mWelcomeTime = 3000L;
    private var mStartTime = 0L

    override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_welcome)
        mStartTime = System.currentTimeMillis()
        if (checkPermission()){
            LLog.v(TAG, "hasPermission")
            iv_welcome.postDelayed({nextPage()}, mWelcomeTime)
        }else{
            LLog.v(TAG, "noPermission")
        }
    }

    private fun nextPage(){
        LLog.v(TAG, "goto next page")
        if (!mSession.isLogin()){
            startActivity(Intent(this, LoginActivity::class.java))
        }else{
            startActivity(Intent(this, MainActivity::class.java))
        }
        finish()
    }

    private fun checkPermission() : Boolean{
        val writePermission = Manifest.permission.WRITE_EXTERNAL_STORAGE
        val permissionResult: Int
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            permissionResult = checkSelfPermission(writePermission)
            if (permissionResult != PackageManager.PERMISSION_GRANTED){

                if (!shouldShowRequestPermissionRationale(writePermission)){
                    requestPermissions(arrayOf(writePermission), 100)
                }else{
                    showForceRequestWriteExternalPermission()
                }
                return false
            }
        }
        return true
    }

    // show dialog for request permission
    private fun showForceRequestWriteExternalPermission(){
        LLog.v(TAG, "You Need WRITE_EXTERNAL_STORAGE permission")
        val dialog =  AlertDialog.Builder(this).setMessage(R.string.need_write_external_storage)
                .setNegativeButton("退出", {dialog, which -> finish() })
                .create()
        dialog.setCancelable(false)
        dialog.show()
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 100){
            // request for write sdcard permission
            val result = grantResults[0]
            if (result == PackageManager.PERMISSION_DENIED){
                showForceRequestWriteExternalPermission()
            }else{
                // permission has been approved
                LLog.init(applicationContext)
                nextPage()
            }
        }
    }
}
